package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.constant.EnergyLevelType;
import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.dto.FindDynamicCommentDTO;
import com.juyu.miliao.api.domain.vo.FindDynamicCommentVO;
import com.juyu.miliao.api.domain.vo.ResultFindDynamicCommentVO;
import com.juyu.miliao.api.service.*;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author xuanqicheng
 */
@RestController
@RequestMapping("/find/dynamic/comment")
@CrossOrigin
public class FindDynamicCommentInfoController {

    @Autowired
    private AppKeyService appKeyService;

    @Autowired
    private FindDynamicCommentService findDynamicCommentService;

    @Autowired
    private CheckFindDynamicCommentAndReplyService checkFindDynamicCommentAndReplyService;

    @Autowired
    private EnergyLevelService energyLevelService;

    @PostMapping("/")
    public FindDynamicCommentVO addFindDynamicCommentInfo(Authentication token, @Validated FindDynamicCommentDTO findDynamicCommentDTO,
                                                          BindingResult bindingResult) throws IOException {
        if (bindingResult.hasErrors()) {
            throw new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();
        findDynamicCommentDTO.setFromUserId(subject.getId());

        Appkey appkey = appKeyService.getAppKeyByKey("findDynamicCommentAndReplyNumber");

        checkFindDynamicCommentAndReplyService.checkNumber(subject.getId(),Integer.parseInt(appkey.getValue()));
        FindDynamicCommentVO findDynamicCommentVo = findDynamicCommentService.addFindDynamicCommentInfo(findDynamicCommentDTO);

        Appkey energyKey = appKeyService.getAppKeyByKey("commentAndReplyObtainEnergy");

        energyLevelService.addUserEnergyLevel(subject.getId(),
                EnergyLevelType.ENERGY_TYPE_COMMENT,Integer.parseInt(energyKey.getValue()));

        return findDynamicCommentVo;


    }

    @GetMapping("/{findId}/{page}-{pageSize}")
    public ResultFindDynamicCommentVO getDynamicInfoList(Authentication token, @PathVariable("findId") Integer findId,
                                                         @PathVariable("page") Integer page,
                                                         @PathVariable("pageSize") Integer pageSize) throws IOException {
        long userId = 0;
        if (token != null) {
            Subject subject = (Subject) token.getPrincipal();
            userId = subject.getId();
        }
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }

        return findDynamicCommentService.selectPage(userId, findId, page, pageSize);
    }
}
